
<template>
	<view style="background-color: #f2f2f7;width: 100vw;" >
		
		<!-- <today today="管理" ref="today"></today> -->
		<view
			style="width: 750rpx;position: fixed;top: 0;left: 0;z-index: 2;background: rgba(255, 255, 255, 0.8);-webkit-backdrop-filter: saturate(180%) blur(10px);backdrop-filter: saturate(180%) blur(5px);"
			v-bind:style="{ height: 'calc(' + naviBarHeight + 'px + 20rpx)' }"
		>
			<view style="width: 100%;" v-bind:style="{ height: statusBarHeight + 'px' }"></view>
			<view
				style="width: 100%;display: flex;justify-content: center;align-items: center;font-size: 32rpx;font-weight: bold;"
				v-bind:style="{ top: statusBarHeight + 'px', height:'calc(' + (naviBarHeight - statusBarHeight) + 'px + 20rpx)', opacity: 1 }"
			>
				<view style="display: flex;flex-direction: row;justify-content: space-between;align-items: center;width: 90%;height: 100%;">
					<view class="flex-row justify-between align-center">
						<view  @click="backTokechengbiao">
							<u-icon name="arrow-left" style='color: #afb0b1;' size="40"/>
						</view>
						<view style="font-size: 40rpx;margin-left: 8rpx;">设置</view>
					</view>
					
					<view class="flex-row justify-between" style="width: 23vw;">
						<view v-if="iflogin && kechengbiao.length" @click="reload" style="color: #000;font-size: 32rpx;height:64rpx;width: 64rpx;display: flex;justify-content: center;align-items: center;">
						
							<u-icon name="reload" color="#000" size="52"></u-icon>
						
						</view>
						<image @click="add" src='../../static/icons/ios-add-circle.svg' style="width: 64rpx;height: 64rpx;"></image>
						
					</view>
				</view>
			</view>
			<view style="width:750rpx;height: 1px; background-color: #c6c6c8;position: absolute;bottom: 0;left: 0;"></view>
		</view>
		<view class="target"></view>
		
		<view v-bind:style="{ height: 'calc(' + naviBarHeight + 'px + 20rpx)' }"></view>
		<!-- <view v-if='add_event.length' style="border-width: 2rpx;border-color: #c6c6c8;border-style: solid;width: 100vw;background-color: #fff;margin-bottom: 15rpx;border-left-width: 0;border-right-width: 0;">
			<view style="width: 100vw;height: 100rpx;display: flex;flex-direction: row;" v-for="(item,index) in add_event" :key='item'>
				<view style="width: 115rpx;height: 100rpx;display: flex;justify-content: center;align-items: center;">
					<view style="width: 32rpx;height: 32rpx;border-radius: 48rpx;" v-bind:style="{backgroundColor:item.color.border_color}"></view>
				</view>
				<view style="width: 635rpx;height: 100rpx;display: flex;flex-direction: column;">
					<view style="width: 635rpx;height: 100rpx;display: flex;justify-content: space-between;align-items: center;">
						<view class="topic_cont_text" style="font-size: 40rpx;">{{item.name}}</view>
						<view @click="changeAddEvent(index)" style="margin-right: 40rpx;">
							<u-icon name="info-circle" color="#eb4d3d" size="56"></u-icon>
						</view>
					</view>
					<view style="width: 635rpx;height: 2rpx;background-color: #c6c6c8;" v-if='index < add_event.length-1'></view>
				</view>
			</view>

		</view> -->
		<!-- <view v-if="add_event.length" style="width: 100vw;margin-bottom: 15rpx;display: flex;justify-content: flex-start;align-items: center;flex-direction: column;color: #000;">
			<view @click="changeAddEvent(index)" style="width: 670rpx;padding:32rpx 40rpx;margin:20rpx 40rpx;display: flex;flex-direction: column;background-color: #fff;border-radius: 28rpx;" v-for="(item,index) in add_event" :key='item'>
				<view style="font-size: 28rpx;color: #89898d;display: flex;flex-direction: row;">{{item.bixiu}}  <view style="color: rgb(36,92,121);">（新增）</view>  </view>
				<view style="font-size: 52rpx;font-weight: bold;width:590rpx;height: 80rpx;overflow-x:scroll;white-space: nowrap;margin-top: 18rpx;">
					{{item.name}}
				</view>
				<view style="display: flex;flex-direction: row;align-items: center;margin-top: 18rpx;">
					<image src="../../static/icons/teacher-head-fill.svg" style="width: 96rpx;height: 96rpx;margin-left: -10rpx;"></image>
					<view style="margin-left: 28rpx;width: 466rpx;display: flex;flex-direction: column;">
						<view style="font-size: 38rpx;">任课老师</view>
						<view style="font-size: 34rpx;color: #8a8a8d;margin-top: 4rpx;">{{item.teacher}}</view>
						
					</view>
				</view>
				<view style="display: flex;flex-direction: row;align-items: center;margin-top: 36rpx;">
					<image src="../../static/icons/ios-time.svg" style="width: 96rpx;height: 96rpx;margin-left: -10rpx;"></image>
					<view style="margin-left: 28rpx;width: 466rpx;display: flex;flex-direction: column;">
						<view style="font-size: 38rpx;">上课时段</view>
						<view v-for="(each_time,each_index) in item.time" style="font-size: 34rpx;color: #8a8a8d;margin-top: 4rpx;">
							{{calcAddWeekType(index,each_index)}} 星期{{weekName[each_time.day]}}  {{each_time.juti[0]+1}}-{{each_time.juti[1]+1}}节
						</view>
					</view>
				</view>
			</view>
		</view> -->
		
		<view v-if="kechengbiao.length" style="width: 100vw;margin-bottom: 15rpx;display: flex;justify-content: flex-start;align-items: center;flex-direction: column;color: #000;">
			<view style="width: 670rpx;padding:32rpx 40rpx;margin:20rpx 40rpx;display: flex;flex-direction: column;background-color: #fff;border-radius: 28rpx;" v-for="(item,index) in kechengbiao" :key='item'>
				<view style="font-size: 28rpx;color: #89898d;">{{item.bixiu}} 学分{{item.credits}}</view>
				<view style="font-size: 52rpx;font-weight: bold;width:590rpx;height: 80rpx;overflow-x:scroll;white-space: nowrap;margin-top: 18rpx;">
					{{item.name}}
				</view>
				<view style="display: flex;flex-direction: row;align-items: center;margin-top: 18rpx;">
					<image src="../../static/icons/teacher-head-fill.svg" style="width: 96rpx;height: 96rpx;margin-left: -10rpx;"></image>
					<view style="margin-left: 28rpx;width: 466rpx;display: flex;flex-direction: column;">
						<view style="font-size: 38rpx;">任课老师</view>
						<view style="font-size: 34rpx;color: #8a8a8d;margin-top: 4rpx;">{{item.teacher}}</view>
						
					</view>
				</view>
				<view style="display: flex;flex-direction: row;align-items: center;margin-top: 36rpx;">
					<image src="../../static/icons/ios-time.svg" style="width: 96rpx;height: 96rpx;margin-left: -10rpx;"></image>
					<view style="margin-left: 28rpx;width: 466rpx;display: flex;flex-direction: column;">
						<view style="font-size: 38rpx;">上课时段</view>
						<view v-for="(each_time,each_index) in item.time" style="font-size: 34rpx;color: #8a8a8d;margin-top: 4rpx;">
							{{calcWeekType(index,each_index)}} 星期{{weekName[each_time.day]}}  {{each_time.juti[0]+1}}-{{each_time.juti[each_time.juti.length - 1]+1}}节
						</view>
						
					</view>
				</view>
				<view style="height: 1rpx;width: 100%;background-color: #bfbfbf;margin-top: 40rpx;"></view>
				<view style="display: flex;flex-direction: row;width: 100%;justify-content: space-between;margin-top: 30rpx;color: #8a8a8d;">
					<view @click="changeKechengbiao(item.id)" style="display: flex;flex-direction: row;align-items: center;"><u-icon  name="edit-pen" color="#8a8a8d" size="28" style="margin-right: 10rpx;"></u-icon>编辑</view>
					<view @click="kechengReload" style="display: flex;flex-direction: row;align-items: center;"><u-icon  name="reload" color="#8a8a8d" size="28" style="margin-right: 10rpx;"></u-icon>更新</view>
					<view @click="Removekecheng(item.id)" style="display: flex;flex-direction: row;align-items: center;"><u-icon name="minus-circle" color="#8a8a8d" size="28" style="margin-right: 10rpx;"></u-icon>删除</view>
					
				</view>
			</view>
		</view>
		
		<view v-if="iflogin && !kechengbiao.length" @click="reload" style="color: #000;font-size: 32rpx;height:100rpx;width: 100vw;display: flex;justify-content: center;align-items: center;padding-bottom: 15rpx;position: absolute;bottom:0;">
		
			重新加载课程表<u-icon name="reload" color="#000" size="40"></u-icon>
		
		</view>
		<view v-if="!kechengbiao.length" style="display: flex;justify-content: center;align-items: center;font-size: 56rpx;color: #c3c3c3;" v-bind:style="{ height: 'calc(100vh - ' + naviBarHeight + 'px' + ' - ' + '176rpx)' }" v-else>
			暂无课程
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				url: '',
				event_list: undefined,
				kechengbiao: undefined,
				add_event: undefined,
				statusBarHeight: undefined,
				naviBarHeight: undefined,
				show_barline: false,
				underTop: 0,
				underBottom: 0,
				naviBarOpacity: 0,
				blank_bottom:0,
				title_bottom:0,
				weekName:['一','二','三','四','五','六','天'],
				addWayArr : ['根据课程号/课程名添加', '自定义添加'],
				addWayIndex : 0,
				iflogin:false
			}
		},
		onShow() {
			this.kechengbiao = uni.getStorageSync('kechengbiao')
			this.iflogin = getApp().globalData.iflogin
			getApp().globalData.kechengbiao = this.kechengbiao 
			console.log(this.kechengbiao)
			this.add_event = uni.getStorageSync('add_event')
			

			this.add_event =  this.add_event.reverse()


		},
		onReady() {
			// console.log(Sortable)
			// var e1 = document.getElementById('items');
			// console.log(e1)
			// var sortable = Sortable.create(e1,{
			// 	animation:150,
			// 	removeCloneOnHide:true
			// })
			var that = this;
			this.statusBarHeight = getApp().globalData.statusBarHeight;
			this.naviBarHeight = getApp().globalData.naviBarHeight;
			console.log('\n\n\n' + this.statusBarHeight);
		},
		methods: {
			Removekecheng(index) {
				var that = this;
				uni.showModal({
				    title: '提示',
				    content: '是否要从课程表中删除该课程？',
				    success: function (res) {
				        if (res.confirm) {
							that.kechengbiao.splice(index, 1);
							console.log('start')
							for (var i = index; i < that.kechengbiao.length; i++) {
								console.log(i)
								that.kechengbiao[i].id = i
							}
							getApp().globalData.kechengbiao = that.kechengbiao
							uni.setStorageSync('kechengbiao',getApp().globalData.kechengbiao)
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
				
				
			},
			backTokechengbiao() {
				uni.navigateBack({})
			},
			changeKechengbiao(index) {
				console.log('kechengbiao')
				// getApp().globalData.current_event_index = index
				getApp().globalData.event = this.kechengbiao[index]
				// getApp().globalData.change_kechengbiao = 1
				uni.navigateTo({
					url: 'change'
				})
			},
			changeAddEvent(index) {
				console.log('event')
				getApp().globalData.event = this.add_event[index]
				getApp().globalData.change_kechengbiao = 0
				uni.navigateTo({
					url: 'change'
				})
			},
			add(res) {
				uni.showActionSheet({
					itemList:['自定义添加','搜索课程以添加'],
					success(res){
						if(res.tapIndex == 0){
							getApp().globalData.event = -1
							// getApp().globalData.change_kechengbiao = 1
							uni.navigateTo({
								url: 'change'
							})	
						}
						else if(res.tapIndex == 1){
							uni.navigateTo({
								url: './changeById'
							})
						}
					}	
				})
			},
			reload() {
				var that = this
				var name = getApp().globalData.username
				var password = getApp().globalData.userpassword
				if (!name || !password) {
					wx.showToast({
						title: '刷新失败，请到“我的”页面重新登录',
						icon: 'none',
						duration: 2000
					});
					return
				}
				uni.showLoading({
					title: '获取课程表中'
				})
				uni.request({
					url: getApp().globalData.URL + 'common_apis/get_kechengbiao',
					data: {
						username: name,
						userpassword: password,
						year: 43,
						term: 1
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					},
					method: 'POST',
					success(res) {
						if (res.data.statusCode == 1000) {
							var kechengbiao = res.data.kechengbiao
							for (var i = 0; i < kechengbiao.length; i++) {
								kechengbiao[i].id = i
								kechengbiao[i].color = {
									id: 3,
									name: '蓝色',
									border_color: '#51aaf2',
									background_color: '#d6edfb',
									font_color: '#2f6691',
									fill_color: '#59b0f3',
									checked: false
								}
				
							}
							getApp().globalData.kechengbiao = kechengbiao
							uni.setStorageSync('kechengbiao', kechengbiao)
							that.kechengbiao = kechengbiao
							
							// #ifdef APP-PLUS
							if (getApp().globalData.platform == 'ios') {
								var UserDefaultsClass = plus.ios.importClass("NSUserDefaults")
								var standardUserDefaults = UserDefaultsClass.standardUserDefaults().initWithSuiteName(
									"group.cyonline.makeabetterworld.cn")
								plus.ios.invoke(standardUserDefaults, "setObject:forKey:", JSON.stringify(getApp().globalData.kechengbiao
									.concat(
										getApp().globalData.add_event)), 'kechengbiao');
								plus.ios.invoke(standardUserDefaults, "synchronize");
							}
							// #endif
							
							
							uni.showToast({
								title: '加载成功',
								duration: 2000
							})
				
						} else {
							uni.showToast({
								title: res.data.error,
								icon: 'none',
								duration: 3000
							})
						}
					}
				})
			},
			calcAddWeekType(index,each_index){
				var week = this.add_event[index].time[each_index].week
				
				const start_week = week[0]
				const end_week = week[week.length - 1]
				var i = start_week;
				var end;
				while (week.indexOf(i) != -1 && week.indexOf(i+1) == -1 ) {
					i = i + 2
					// if ( this.checkList[i].checkStyle != 'check' || this.checkList[i+1].checkStyle != '' ) {
					// 	end = i-2
					// 	break;
					// }
					if(i == end_week){
						if (i % 2){
							return (start_week + 1) + '-' + (end_week + 1) + '周双周'
						}
						else{
							return (start_week + 1) + '-' + (end_week + 1) + '周单周'
						}
					}
				}
			
				for (i = start_week;i<end_week+1;i++){
					if (week.indexOf(i)== -1){
						break
					}
				}
				if(i == end_week+1){
					return (start_week + 1) + '-' + (end_week + 1) + '周全周'
				}
				return '自定义'
				
			},
			calcWeekType(index,each_index){
				var week = this.kechengbiao[index].time[each_index].week
				
				const start_week = week[0]
				const end_week = week[week.length - 1]
				var i = start_week;
				var end;
				while (week.indexOf(i) != -1 && week.indexOf(i+1) == -1 ) {
					i = i + 2
					// if ( this.checkList[i].checkStyle != 'check' || this.checkList[i+1].checkStyle != '' ) {
					// 	end = i-2
					// 	break;
					// }
					if(i == end_week){
						if (i % 2){
							return (start_week + 1) + '-' + (end_week + 1) + '周双周'
						}
						else{
							return (start_week + 1) + '-' + (end_week + 1) + '周单周'
						}
					}
				}

				for (i = start_week;i<end_week+1;i++){
					if (week.indexOf(i)== -1){
						break
					}
				}
				if(i == end_week+1){
					return (start_week + 1) + '-' + (end_week + 1) + '周全周'
				}
				return '自定义'
				
			},
		}
	}
</script>
<style>
	page{
		background-color:#f2f2f7;
	}
	.topic_cont_text {
		overflow: hidden;
		word-break: break-all;
		/* break-all(允许在单词内换行。) */
		text-overflow: ellipsis;
		/* 超出部分省略号 */
		display: -webkit-box;
		/** 对象作为伸缩盒子模型显示 **/
		-webkit-box-orient: vertical;
		/** 设置或检索伸缩盒对象的子元素的排列方式 **/
		-webkit-line-clamp: 1;
		/** 显示的行数 **/

	}
	.target {
		width: 750rpx;
		height: 10rpx;
	}
</style>
